﻿using System;
using System.Collections.Generic;
using Glue;
using Xunit;

namespace GlueExamples.ReportedIssues
{
    /// <summary>
    /// Item #	7572 
    /// Reported By	thiagoburgo
    /// </summary>
    public class ProblemWithUnsetNestedEnumerables
    {
        [Fact]
        public void Run()
        {
            var fsi = new FileSystemInfo();
            fsi.FileSystemInfos = new List<FileSystemInfo>()
                {
                    new FileSystemInfo
                        {
                            Parent = fsi, FileSystemInfos = null, FullName = "Exams\\Microsoft", IsDirectory = true
                        },
                    new FileSystemInfo { 
                        Parent = fsi, FullName = "Exams\\Oracle", IsDirectory = true, 
                        FileSystemInfos = new List<FileSystemInfo> {
                            new FileSystemInfo(){ Parent = null, FullName = "Exams\\Oracle\\teste.vce", IsDirectory = false}
                        } 
                    },
                };
            fsi.FileSystemInfos[1].FileSystemInfos[0].Parent = fsi.FileSystemInfos[1];

            fsi.FullName = "Exams";
            fsi.IsDirectory = true;

            var mapper = new Mapping<FileSystemInfo, FileSystemViewModel>(x => new FileSystemInfo(), y => new FileSystemViewModel());
            mapper.AutoRelateEqualNames();
            mapper.Relate(l => l.FileSystemInfos, r => r.FileSystemInfos, mapper);
            //mapper.Relate(l => l.Parent, r => r.Parent, mapper);


            FileSystemViewModel model = mapper.Map(fsi);
            Assert.Equal(2, model.FileSystemInfos.Count);
            Assert.Equal(1, model.FileSystemInfos[1].FileSystemInfos.Count);
        }
    }

    public class FileSystemInfo
    {
        public FileSystemInfo()
        {
            this.Parent = null;
            this.FileSystemInfos = new List<FileSystemInfo>();
        }
        public bool IsDirectory { get; set; }
        public FileSystemInfo Parent { get; set; }
        public string FullName { get; set; }
        public string Name
        {
            get
            {
                String name = "";
                if (FullName != null && FullName.Trim().Length > 0)
                {
                    String[] dirsInPath = this.FullName.Split(new[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                    if (dirsInPath.Length > 0)
                    {
                        name = dirsInPath[dirsInPath.Length - 1];
                    }
                }
                return name;
            }
        }
        public List<FileSystemInfo> FileSystemInfos { get; set; }
    }
    public class FileSystemViewModel
    {
        public bool IsDirectory { get; set; }
        public String ImagePath
        {
            get
            {
                return this.IsDirectory ? "/Resources/Images/AppBar/appbar.folder.rest.png"
                                        : "/Resources/Images/AppBar/appbar.clipboard.edit.png";
            }
        }
        public FileSystemViewModel Parent { get; set; }
        public string FullName { get; set; }
        public string Name { get; set; }
        public List<FileSystemViewModel> FileSystemInfos { get; set; }
    }
}
